package com.zj.osm.view.map.map;


import android.util.Log;

import com.zj.osm.App;
import com.zj.osm.bean.AddMarkerAndLine;
import com.zj.osm.bean.AddMarkerAndLinePol;
import com.zj.osm.bean.DefaultBean;
import com.zj.osm.bean.ReturnLineAndPoint;
import com.zj.osm.entity.db.LineDao;
import com.zj.osm.entity.db.PointDao;
import com.zj.osm.entity.greendao.Line;
import com.zj.osm.entity.greendao.Point;
import com.zj.osm.entity.greendao.Project;
import com.zj.osm.model.pojo.Equip;
import com.zj.osm.model.pojo.ModuleType;
import com.zj.osm.mvp.BasePresenter;
import com.zj.osm.utils.db_utils.EquipUtils;
import com.zj.osm.utils.db_utils.LineUtils;
import com.zj.osm.utils.db_utils.PointsUtils;

import org.osmdroid.api.IGeoPoint;
import org.osmdroid.util.GeoPoint;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.zj.osm.App.getInstances;
import static com.zj.osm.utils.MyUtils.Distance;

/**
 * 地图P层 持有M层和V层的引用
 *
 * @author jiang zhu on 2019/10/26
 */
class MapActivityPresenter extends BasePresenter<MapActivityView> {

    private MapActivityModel mapModel;

    MapActivityPresenter() {
        this.mapModel = new MapActivityModel();
    }

    /**
     * 获取当前项目中所有点的中点
     *
     * @param latList  纬度的集合
     * @param longList 经度的集合
     */
    void toPointsLocation(List<Double> latList, List<Double> longList) {
        GeoPoint geoPoint = mapModel.toPointsLocation(latList, longList);
        if (getView() != null) {
            getView().toPointsLocation(geoPoint);
        }
    }

    /**
     * 新增一个点到数据库
     *
     * @param id        项目id
     * @param id        uuid
     * @param icon      图片（设备类型）
     * @param mapCenter 经纬度
     * @param pointList 点的集合
     */
    void addPointAndLine(Long id, String icon, IGeoPoint mapCenter, List<Point> pointList) {
        Point point = mapModel.addPoint(id, icon, mapCenter);
        Line line = null;
        if (pointList.size() > 0) {
            line = mapModel.addLine(id, point, pointList, mapCenter);
        }
        if (getView() != null) {
            //getView().addPoint(point, line);
        }
    }

    /**
     * 同杆并架
     *
     * @param id               项目id
     * @param withTheLeverList 线
     */
    void withTheLeverList(Long id, List<String> withTheLeverList) {
        Line line = mapModel.addWithTheLever(id, withTheLeverList);
        if (getView() != null) {
            // getView().addWithTheLever(line);
        }
    }

    /**
     * 初始化获得项目有关信息
     *
     * @param projectId 项目id
     */
    void initPointAndLine(Long projectId) {
        Project project = mapModel.getProjectById(projectId);
        List<Point> pointList = mapModel.getPointListById(projectId);
        List<Line> lineList = mapModel.getLineListById(projectId);
        List<Line> withTheLeverLineListById = mapModel.getWithTheLeverLineListById(projectId);
        if (getView() != null) {
            getView().initPointAndLine(project, pointList, lineList, withTheLeverLineListById);
        }
    }

    /**
     * 删除线
     *
     * @param lineId 线的id
     */
    void deleteLine(String lineId) {
        mapModel.deleteLine(lineId);
    }

    /**
     * 删除点
     *
     * @param pointId 点的id
     */
    void deletePoint(String pointId) {
        Point point = mapModel.deletePoint(pointId);
        List<Line> lineList = mapModel.deletePointAndLine(point);
        if (getView() != null) {
            //getView().deletePoint(point, lineList);
        }
    }


    /**
     * 继续勘测
     * 通过获取当前点的id，查出后进行list中位置的调换
     *
     * @param id
     */
    public void continuingSurvey(String id) {
        Point point = mapModel.continuingSurvey(id);
        if (getView() != null) {
            getView().continuingSurvey(point);
        }
    }

    /**
     * 画杆塔和线全在这里操作了
     * intint high, Marker currentMarker, IGeoPoint mapCenter, EquipmentCateGoryEmm equipmentCateGoryEmm) {
     */
    public void saveMarkerAndLine(AddMarkerAndLine addMarkerAndLine) {
        ReturnLineAndPoint returnLineAndPoint = mapModel.addMarkerAndLine(addMarkerAndLine.getContext(), addMarkerAndLine.getProject(), addMarkerAndLine.isTfenjieState(), addMarkerAndLine.getWHETHER_TO_SHOW_GUIDES(), addMarkerAndLine.getHigh(),addMarkerAndLine.getAddress(), addMarkerAndLine.getCurrentMarker(), addMarkerAndLine.getMapCenter(), addMarkerAndLine.getEquipmentCateGoryEmm());
        /**-----------------------------------------  线的操作  -------------------------End-----------------------------------**/
        if (returnLineAndPoint == null) {
            return;
        }
        //这里通知View层进行绘制线
        if (returnLineAndPoint.getLine() != null) {
            getView().drawMyRoute(returnLineAndPoint.getLine());
        }
        //这里去添加默认的模块设备
        ArrayList<ModuleType> moduleTypeInsert = EquipUtils.getInstance().selectedEquipModleType(returnLineAndPoint.getEqupId());
        if (moduleTypeInsert.size() > 0) {
            EquipUtils.getInstance().addEquipment(addMarkerAndLine.getProject().getId(), returnLineAndPoint.getPoint().getId(), moduleTypeInsert, 0);
        }
        //默认添加柱状设备
        ArrayList<ModuleType> moduleZhuzData = EquipUtils.getInstance().selectedEquipModleTypeZhuzhang(returnLineAndPoint.getEqupId());//2
        if (moduleTypeInsert.size() > 0) {
            EquipUtils.getInstance().addEquipment(addMarkerAndLine.getProject().getId(), returnLineAndPoint.getPoint().getId(), moduleZhuzData, 2);
        }
        //TODO 通知vive层添加数据且添加View设置jixukance为false。
        getView().drawMarker(returnLineAndPoint.getPoint(),returnLineAndPoint.getLine(),false);
        List<DefaultBean> defaultBeanList = PointsUtils.Companion.getDefault(EquipUtils.getInstance().getId(returnLineAndPoint.getPoint().getSblx()));
        if (defaultBeanList.size() > 0) {
            for (int i = 0, length = defaultBeanList.size(); i < length; i++) {
                if (defaultBeanList.get(i).getType().equals("杆塔设备")) {
                    EquipUtils.getInstance().addEquipment(defaultBeanList.get(i).getModelTypeName(), returnLineAndPoint.getPoint().getId(), 0);
                } else if (defaultBeanList.get(i).getType().equals("柱上设备")) {
                    EquipUtils.getInstance().addEquipment(defaultBeanList.get(i).getModelTypeName(), returnLineAndPoint.getPoint().getId(), 2);
                }
            }

        }


    }

    public void saveMarkerAndLineTopographic(AddMarkerAndLinePol addMarkerAndLine) {
        ReturnLineAndPoint returnLineAndPoint = mapModel.addMarkerAndLineTopy(addMarkerAndLine.getEquipmentCateGoryEmm(),addMarkerAndLine.getContext(), addMarkerAndLine.getProject(), addMarkerAndLine.getJixuKance(), addMarkerAndLine.getWHETHER_TO_SHOW_GUIDES(), addMarkerAndLine.getHigh(),addMarkerAndLine.getAddress(), addMarkerAndLine.getCurrentMarker(), addMarkerAndLine.getMapCenter(), addMarkerAndLine.getJpoint());
        /**-----------------------------------------  线的操作  -------------------------End-----------------------------------**/
        if (returnLineAndPoint.getLine() != null) {
            //画线
            //getView().drawMyRoute(returnLineAndPoint.getLine());
        }
        //这里去添加默认的模块设备
        ArrayList<ModuleType> moduleTypeInsert = EquipUtils.getInstance().selectedEquipModleType(returnLineAndPoint.getEqupId());
        if (moduleTypeInsert.size() > 0) {
            EquipUtils.getInstance().addEquipment(addMarkerAndLine.getProject().getId(), returnLineAndPoint.getPoint().getId(), moduleTypeInsert, 0);
        }
        //默认添加柱状设备
        ArrayList<ModuleType> moduleZhuzData = EquipUtils.getInstance().selectedEquipModleTypeZhuzhang(returnLineAndPoint.getEqupId());
        if (moduleZhuzData.size() > 0) {
            EquipUtils.getInstance().addEquipment(addMarkerAndLine.getProject().getId(), returnLineAndPoint.getPoint().getId(), moduleZhuzData, 2);
        }
        //跟新Marker
        getView().drawMarker(returnLineAndPoint.getPoint(),returnLineAndPoint.getLine(),false);
    }
}
